

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 一、参考资料 视频资料 二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6 Bootstrap 5.1.3  三、Vuex@3插件 注意：这里的Vuex知识是基于Vuex版本3的。  Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。对 vue 应用中多个组件的共">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据">
<meta property="og:url" content="http://example.com/2022/06/24/Vue2%20%E7%AC%94%E8%AE%B04%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BF%A1%E6%81%AF%E5%A2%9E%E5%88%A0%E6%9F%A5%E6%A1%88%E4%BE%8B%20_%20Vuex%E7%BB%84%E4%BB%B6%E7%AE%80%E4%BB%8B%E3%80%81%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%20_%20Vuex%E7%9A%84%E4%B8%89%E5%B1%82%E7%BB%93%E6%9E%84%E2%80%94%E2%80%94store%E8%B4%9F%E8%B4%A3%E5%93%8D%E5%BA%94%E7%BB%84%E4%BB%B6%E3%80%81mutations%E8%B4%9F%E8%B4%A3%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E3%80%81state%E8%B4%9F%E8%B4%A3%E5%AD%98%E5%82%A8%E6%95%B0%E6%8D%AE/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 一、参考资料 视频资料 二、运行环境  Windows11 Visual Studio Code v2022 Node.js v16.5.01 Vue&#x2F;cli v5.0.6 Bootstrap 5.1.3  三、Vuex@3插件 注意：这里的Vuex知识是基于Vuex版本3的。  Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。对 vue 应用中多个组件的共">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/d6703f732b834c69bf7ea76471851390.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/38fe63970cc14200bf7faad533591056.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/42b1262af2094e76a6d62053dfddabf7.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/955c9b2d51044869b456042cd8cbbc0b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/11d7eda039c64f54bdddb4a1f23e462f.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/bec65e716d4448e09405c83d3e969868.gif#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4725f8cec9ef41dfad888492d5b5dbcd.png">
<meta property="article:published_time" content="2022-06-24T12:31:41.000Z">
<meta property="article:modified_time" content="2022-08-22T15:49:32.291Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/d6703f732b834c69bf7ea76471851390.png">
  
  
  
  <title>Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-06-24 20:31" pubdate>
          2022年6月24日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          24k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          200 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="一、参考资料"><a href="#一、参考资料" class="headerlink" title="一、参考资料"></a>一、参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Zy4y1K7SH?p=105">视频资料</a></p>
<h1 id="二、运行环境"><a href="#二、运行环境" class="headerlink" title="二、运行环境"></a>二、运行环境</h1><hr>
<ul>
<li>Windows11</li>
<li>Visual Studio Code v2022</li>
<li>Node.js v16.5.01</li>
<li>Vue&#x2F;cli v5.0.6</li>
<li>Bootstrap 5.1.3</li>
</ul>
<h1 id="三、Vuex-3插件"><a href="#三、Vuex-3插件" class="headerlink" title="三、Vuex@3插件"></a>三、Vuex@3插件</h1><hr>
<p>注意：这里的Vuex知识是基于Vuex版本3的。</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/"><strong>Vuex</strong></a> 是一个专为 Vue.js 应用程序开发的状态管理模式。对 vue 应用中多个组件的共享状态进行集中式的管理（读&#x2F;写），也是一种组件间通信的方式，且适用于任意组件间通信。</p>
</blockquote>
<p>Vuex插件的GitHub地址：<a target="_blank" rel="noopener" href="https://github.com/vuejs/vuex">点击查看</a></p>
<p><strong>适用场景：</strong></p>
<ol>
<li>多个Vue组件依赖于同一状态 </li>
<li>来自不同Vue组件的行为需要变更同一状态</li>
</ol>
<h2 id="3-1-状态管理模式"><a href="#3-1-状态管理模式" class="headerlink" title="3.1 状态管理模式"></a>3.1 状态管理模式</h2><p>Vue官方提供的案例：<a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/#%E4%BB%80%E4%B9%88%E6%98%AF-%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86%E6%A8%A1%E5%BC%8F"><strong>计数</strong></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-comment">// state 驱动应用的数据源</span><br>  data () &#123;<br>    <span class="hljs-keyword">return</span> &#123;<br>      <span class="hljs-attr">count</span>: <span class="hljs-number">0</span><br>    &#125;<br>  &#125;,<br>  <span class="hljs-comment">// view 以声明方式将 state 映射到视图</span><br>  <span class="hljs-attr">template</span>: <span class="hljs-string">`</span><br><span class="hljs-string">    &lt;div&gt;&#123;&#123; count &#125;&#125;&lt;/div&gt;</span><br><span class="hljs-string">  `</span>,<br>  <span class="hljs-comment">// actions 响应在 view 上的用户输入导致的状态变化</span><br>  <span class="hljs-attr">methods</span>: &#123;<br>    increment () &#123;<br>      <span class="hljs-variable language_">this</span>.<span class="hljs-property">count</span>++<br>    &#125;<br>  &#125;<br>&#125;)<br></code></pre></td></tr></table></figure>
<p>这个状态自管理应用包含以下三个部分：state、view、actions</p>
<p>以下是一个表示“单向数据流”理念的简单示意：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/c15ef24364404135b876f4091b7f9c69.png#pic_center">https://img-blog.csdnimg.cn/c15ef24364404135b876f4091b7f9c69.png#pic_center</a> &#x3D;50%x)<br>当应用遇到多个组件共享状态时，单向数据流的简洁性很容易被破坏。</p>
<p><strong>Vuex设计思路：</strong></p>
<p>将Vue组件的共享状态抽取出来，以一个全局 <strong>单例模式</strong> 管理。</p>
<p>在这种模式下，组件树就构成了一个巨大的“视图”，不管在树的哪个位置，任何组件都能获取状态或者触发行为。</p>
<blockquote>
<p>据官方说明， Vuex的设计借鉴了 <a target="_blank" rel="noopener" href="https://facebook.github.io/flux/docs/overview/">Flux</a>、<a target="_blank" rel="noopener" href="https://redux.js.org/">Redux</a> 和 <a target="_blank" rel="noopener" href="https://guide.elm-lang.org/architecture/">The Elm Architecture</a></p>
</blockquote>
<p><strong>Vuex结构图：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/53f0bd7a79064ceca4db08dfb4798788.png#pic_center">https://img-blog.csdnimg.cn/53f0bd7a79064ceca4db08dfb4798788.png#pic_center</a> &#x3D;80%x)<br>如果拿SpringMVC架构和这张图做对比，那么会发现它们有类似的地方，如下图：</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/87f28608f97a45e8937185ea0f3ea63c.png#pic_center">https://img-blog.csdnimg.cn/87f28608f97a45e8937185ea0f3ea63c.png#pic_center</a> &#x3D;80%x)</p>
<ul>
<li>$Actions$层负责响应请求，同时执行一些业务逻辑，类似于后端的$Controller+Service$ ，在 $Actions$ 层通常会适用 <code>axios</code> 发送请求到后端获取数据，同时可以调用 <code>API</code> 获取数据。</li>
<li>$Mutations$层负责进行数据的处理，类似于后端的 $DAO$ 层，它会根据 $Actions$ 层传来的数据对存储在$state$层的数据进行增删改查之类的操作。</li>
<li>$State$ 层负责存储数据，在Vue中通常用于渲染页面。</li>
</ul>
<p>因为Vuex插件支持所有组件共享<code>$store</code>这个对象，所以每个组件都可以通过<code>$sotre</code>获取到 $state$ 层里的数据。</p>
<p>而组件可以直接获取数据，另外还可以通过 <strong>计算属性</strong> 获取到state层的数据，例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">computed</span>: &#123;<br>  <span class="hljs-title function_">studentList</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">state</span>.<span class="hljs-property">student</span>.<span class="hljs-property">studentList</span> || []<br>  &#125;,<br>&#125;,<br></code></pre></td></tr></table></figure>
<p>在使用Vuex3 时，我们需要按照其设计的流程来执行，其中 $Mutations$层是必须要经过的，因为Devtools调试工具会监听这一层的变化而不会监听Actions的变化。</p>
<p>除此之外，适用Vuex3的调用流程通常有两种：</p>
<ul>
<li>$actions -&gt; mutations -&gt; state$</li>
<li>$mutations -&gt; state$</li>
</ul>
<p>我们可以忽略actions，这个就和后端的Controller和Service不同了，区别在于前端的actions层主要是负责请求获取数据的，如果无需请求数据，比如：根据当前state里的数据进行筛选查询，此时就不需要请求获取数据，那么Vuex支持直接执行mutations层的代码，直接处理state的数据即可。</p>
<h2 id="3-2-Vuex-核心概念和API"><a href="#3-2-Vuex-核心概念和API" class="headerlink" title="3.2 Vuex 核心概念和API"></a>3.2 Vuex 核心概念和API</h2><h3 id="3-2-1-state"><a href="#3-2-1-state" class="headerlink" title="3.2.1 state"></a>3.2.1 <a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/guide/state.html#state">state</a></h3><p><strong>概念与特点：</strong></p>
<ol>
<li>vuex 管理的状态对象 </li>
<li>唯一，不可重复</li>
</ol>
<p><strong>例如：</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">state</span>: &#123;<br>        <span class="hljs-comment">// 若有本地缓存就读缓存，否则设置为空</span><br>        <span class="hljs-attr">studentList</span>: <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>)) || []<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3-2-3-actions"><a href="#3-2-3-actions" class="headerlink" title="3.2.3 actions"></a>3.2.3 <a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/guide/actions.html#action">actions</a></h3><blockquote>
<p>注: Action是Vuex运行原理图中Vue组件使用Vuex的入口，Mutation也可以是入口。</p>
</blockquote>
<p> <strong>概念与特点：</strong></p>
<ol>
<li>值为一个对象，包含多个响应用户动作的回调函数 </li>
<li>通过 <strong>commit()</strong> 来触发 <strong>mutation</strong> 中函数的调用, 间接更新 <strong>state</strong></li>
<li>action 提交的是 mutation，而不是直接变更状态。</li>
<li>action 可以包含任意异步操作。支持AJAX、定时器等异步操作。</li>
</ol>
<p><strong>重点：触发 actions 中的回调函数</strong></p>
<p>在组件中使用:<code>$store.dispatch(&#39;对应的 action 回调名&#39;)</code> 触发，例如（查询学生信息）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// search.js</span><br><span class="hljs-attr">actions</span>: &#123;<br>    <span class="hljs-title function_">searchStuById</span>(<span class="hljs-params">context, info</span>)&#123;<br>    <span class="hljs-comment">// 业务逻辑比较简单，这里可以发送axios请求获取数据</span><br>        context.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;getStuById&#x27;</span>,info)<br>    &#125;,<br>    ...<br>&#125;<br><span class="hljs-comment">// Search.Vue</span><br><span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&#x27;search/searchStuById&#x27;</span>, [<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])<br></code></pre></td></tr></table></figure>
<p>注意：actions中的回调函数第一个参数默认为应用上下文对象，第二个参数才是通过dispatch传递的值，应用上下文对象内容如下：<br><img src="https://img-blog.csdnimg.cn/d6703f732b834c69bf7ea76471851390.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>这表示我们可以通过这个应用上下文对象继续 dispatch 分发，也可以commit提交到mutation等。</p>
<h3 id="3-2-4-mutations"><a href="#3-2-4-mutations" class="headerlink" title="3.2.4 mutations"></a>3.2.4 <a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/guide/mutations.html#mutation">mutations</a></h3><p><strong>概念与特点：</strong></p>
<ol>
<li>值是一个对象，包含多个直接更新 state 的方法</li>
<li>每个 mutation 都有一个<strong>字符串</strong>的 <strong>事件类型 (type)</strong> 和 一个 **回调函数 (handler)**。</li>
<li>有两种调用方式：1）在 action 中（由第一个参数）使用：commit(‘对应的 mutations 方法名’) 触发 </li>
<li>mutations 中方法的特点：1) 不能写异步代码；2) 只能单纯的操作 state</li>
</ol>
<p>例如（根据ID查询学生信息）：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"> <span class="hljs-attr">mutations</span>: &#123;<br>     <span class="hljs-comment">// 根据ID查询学生</span><br>     <span class="hljs-title function_">getStuById</span>(<span class="hljs-params">state, info</span>)&#123;<br>         <span class="hljs-keyword">let</span> id = info[<span class="hljs-number">0</span>]<br>         <span class="hljs-keyword">let</span> studentList = info[<span class="hljs-number">1</span>]<br>         state.<span class="hljs-property">searchStudentList</span> = studentList.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span>  stu.<span class="hljs-property">id</span> === id)<br>     &#125;,<br>     ...<br>&#125;<br></code></pre></td></tr></table></figure>

<p>这里提一句，在mutation修改完数据后，state里的数据就发生改变了，那么页面里的数据是如何受到影响的呢？</p>
<p>因为Vuex支持所有组件共享同一个 <code>$store</code> ，所以最简单的做法就是可以写一个计算属性，去获取 <code>$store</code> 里state层的数据。例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-attr">computed</span>: &#123;<br>  <span class="hljs-title function_">searchStudentList</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">state</span>.<span class="hljs-property">search</span>.<span class="hljs-property">searchStudentList</span> || []<br>  &#125;,<br>  ...<br>&#125;<br></code></pre></td></tr></table></figure>
<p>前端页面渲染只需要遍历计算属性的名称即可：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(stu) in searchStudentList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;stu.id&quot;</span>&gt;</span><br>    &#123;&#123;stu.id&#125;&#125;, &#123;&#123;stu.name&#125;&#125;<br>  <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="3-2-5-getters"><a href="#3-2-5-getters" class="headerlink" title="3.2.5 getters"></a>3.2.5 <a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/guide/getters.html#getter">getters</a></h3><p><strong>概念与特点：</strong></p>
<ol>
<li>值为一个对象，包含多个用于返回数据的函数</li>
<li>调用方式 —— $store.getters.xxx</li>
<li>可以认为是 store 的计算属性</li>
<li>类似于计算属性，getter 的返回值会根据其依赖被 <strong>缓存</strong> 起来，且只有当其依赖值 <strong>发生了改变</strong> 才会被重新计算。</li>
<li>跟之前的state、action和mutation一样，可以在任意组件中访问，例如：</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// student.js</span><br><span class="hljs-attr">getters</span>: &#123;<br>	studentCount (state)&#123;<br>		<span class="hljs-keyword">return</span> state.<span class="hljs-property">studentList</span>.<span class="hljs-property">lenth</span><br>	&#125;<br>&#125;<br><span class="hljs-comment">// Student.vue</span><br><span class="hljs-attr">computed</span>: &#123;<br>  studentCount () &#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">getters</span>.<span class="hljs-property">studentCount</span> <br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="3-3-Vuex-模块化开发"><a href="#3-3-Vuex-模块化开发" class="headerlink" title="3.3 Vuex 模块化开发"></a>3.3 Vuex 模块化开发</h2><p> <a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/guide/modules.html#module"><strong>modules</strong></a></p>
<p><strong>概念与特点：</strong></p>
<ol>
<li>Vuex支持在modules里设置多个 module </li>
<li>一个 module 是一个 store 的配置对象 </li>
<li>与每个组件（包含有共享数据）对应</li>
</ol>
<p>如下图所示，Vuex通常可以分为不同的模块，比如这里比较简单的分为了学生模块和查询模块。</p>
<p><img src="https://img-blog.csdnimg.cn/38fe63970cc14200bf7faad533591056.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>index.js</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 该文件用于创建 VueX 中最为核心的 Store</span><br><span class="hljs-comment">// 引入 Vue</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-comment">// 安装vuex@3 npm i vuex@3</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vuex</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vuex&#x27;</span><br><span class="hljs-keyword">import</span> search <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./search&#x27;</span><br><span class="hljs-keyword">import</span> student <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./student&#x27;</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Vuex</span>)<br><br><span class="hljs-comment">// 创建 Store并导出</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vuex</span>.<span class="hljs-title class_">Store</span>(&#123;<br>    <span class="hljs-attr">modules</span>: &#123;search, student&#125;<br>&#125;)<br></code></pre></td></tr></table></figure>
<p><strong>search.js</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>    <span class="hljs-attr">namespaced</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-comment">// Actions —— 用于响应组件中的动作</span><br>    <span class="hljs-attr">actions</span>: &#123; ... &#125;,<br>    <span class="hljs-comment">// Mutations —— 用于操作数据（state）</span><br>    <span class="hljs-attr">mutations</span>: &#123; ... &#125;,<br>    <span class="hljs-comment">// State —— 用于存储数据</span><br>    <span class="hljs-attr">state</span>: &#123; ... &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>模块开发引入的js文件中必须有<code> namespaced: true</code> 这一项，否则多个module之间会发生混淆。</p>
<p><font color="red">注: 在使用模块开发后，手动调用dispatch，commit方法时候需要在函数名前加上对应的模块名。</font></p>
<p>例如：根据ID查询学生信息</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&#x27;search/searchStuById&#x27;</span>, [<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])<br></code></pre></td></tr></table></figure>

<h2 id="3-4-Vuex-简化的Map四件套"><a href="#3-4-Vuex-简化的Map四件套" class="headerlink" title="3.4 Vuex 简化的Map四件套"></a>3.4 Vuex 简化的Map四件套</h2><p>这四件套分别是：</p>
<ul>
<li>mapState</li>
<li>mapGetters</li>
<li>mapMutations</li>
<li>mapActions</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://v3.vuex.vuejs.org/zh/guide/state.html#%E7%BB%84%E4%BB%B6%E4%BB%8D%E7%84%B6%E4%BF%9D%E6%9C%89%E5%B1%80%E9%83%A8%E7%8A%B6%E6%80%81"><strong>组件仍然保有局部状态</strong></a></p>
<blockquote>
<p>使用 Vuex 并不意味着你需要将所有的状态放入 Vuex。虽然将所有的状态放到 Vuex 会使状态变化更显式和易调试，但也会使代码变得冗长和不直观。如果有些状态严格属于单个组件，最好还是作为组件的局部状态。你应该根据你的应用开发需要进行权衡和确定。</p>
</blockquote>
<p>上述的四个map函数主要是为了方便将 $store里的属性放到组件里的，接下来就是它们的原型与简化方式：</p>
<p>案例：</p>
<p><strong>student.js</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> &#123; nanoid &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;nanoid&quot;</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>    <span class="hljs-attr">namespaced</span>:<span class="hljs-literal">true</span>,<br>    <span class="hljs-comment">// Actions —— 用于响应组件中的动作（若无Axios可不使用）</span><br>    <span class="hljs-attr">actions</span>: &#123;&#125;,<br>    <span class="hljs-comment">// Mutations —— 用于操作数据（state）</span><br>    <span class="hljs-attr">mutations</span>: &#123;<br>        <span class="hljs-comment">// 添加学生</span><br>        <span class="hljs-title function_">addStudent</span>(<span class="hljs-params">state, stuName</span>)&#123;<br>            state.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">unshift</span>(&#123;<br>                <span class="hljs-attr">id</span>: <span class="hljs-title function_">nanoid</span>().<span class="hljs-title function_">substring</span>(<span class="hljs-number">0</span>, <span class="hljs-number">5</span>),<br>                <span class="hljs-attr">name</span>: stuName, <br>            &#125;)<br>        &#125;,<br>        <span class="hljs-comment">// 根据ID删除学生</span><br>        <span class="hljs-title function_">removeStuById</span>(<span class="hljs-params">state, id</span>)&#123;<br>            state.<span class="hljs-property">studentList</span> = state.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">filter</span>(<br>                <span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span> stu.<span class="hljs-property">id</span> !== id<br>            )<br>        &#125;<br>    &#125;,<br>    <span class="hljs-comment">// State —— 用于存储数据</span><br>    <span class="hljs-attr">state</span>: &#123;<br>        <span class="hljs-comment">// 若有本地缓存就读缓存，否则就初始化三个学生数据</span><br>        <span class="hljs-attr">studentList</span>: <br>        <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>)) || <br>        [<br>            &#123;<br>            <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-string">&#x27;1&#x27;</span>,<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;张三&#x27;</span>,<br>            &#125;,<br>            &#123;<br>            <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-string">&#x27;2&#x27;</span>,<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;李四&#x27;</span>,<br>            &#125;,<br>            &#123;<br>            <span class="hljs-string">&#x27;id&#x27;</span>:<span class="hljs-string">&#x27;3&#x27;</span>,<br>            <span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&#x27;王五&#x27;</span>,<br>            &#125;<br>        ],<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>注意，接下来的案例将使用到ES6的拓展运算符：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">let</span> obj =&#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;uni&#x27;</span>,<br>    <span class="hljs-attr">age</span>: <span class="hljs-string">&#x27;22&#x27;</span><br>&#125;<br><span class="hljs-keyword">let</span> arr = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;@obj&#x27;</span>, &#123;obj&#125;)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;@arr&#x27;</span>, ...arr)<br></code></pre></td></tr></table></figure>
<p><strong>运行效果：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/42b1262af2094e76a6d62053dfddabf7.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>可理解为 <code>...</code> 会将可迭代的对象（包括数组、Map、Set）拆分，拆分后的结果可以重新放到对应的类型中，比如 { …obj } 或 […arr]  。</p>
<p>通过这个方式再加上 <a target="_blank" rel="noopener" href="https://www.runoob.com/w3cnote/deconstruction-assignment.html">ES6的解构赋值</a> 可以用于多个可迭代对象的合并，例如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> obj1 =&#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;uni&#x27;</span>,<br>&#125;<br><span class="hljs-keyword">var</span> obj2 = &#123;<br>    <span class="hljs-attr">age</span>: <span class="hljs-string">&#x27;22&#x27;</span><br>&#125;<br><span class="hljs-keyword">var</span> arr1 = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>]<br><span class="hljs-keyword">var</span> arr2 = [<span class="hljs-number">3</span>]<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;@obj&#x27;</span>, &#123;...obj1, ...obj2&#125;)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;@arr&#x27;</span>, [...arr1, ...arr2])<br></code></pre></td></tr></table></figure>
<p><strong>运行效果：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/955c9b2d51044869b456042cd8cbbc0b.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>在了解到ES6拓展运算符和解构赋值的特点后，接下来就是Vuex提供的map函数的运用了。</p>
<h3 id="3-4-1-mapState"><a href="#3-4-1-mapState" class="headerlink" title="3.4.1 mapState"></a>3.4.1 mapState</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用前，按需在对应的组件里引入。</span><br><span class="hljs-keyword">import</span> &#123;mapState&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vuex&#x27;</span><br><br><span class="hljs-comment">// 手动写法</span><br><span class="hljs-attr">computed</span>:&#123;<br>	<span class="hljs-title function_">studentList</span>(<span class="hljs-params"></span>)&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">state</span>.<span class="hljs-property">student</span>.<span class="hljs-property">studentList</span><br>	&#125;<br>&#125;<br><span class="hljs-comment">// 使用mapState</span><br><span class="hljs-attr">computed</span>:&#123;<br>	<span class="hljs-comment">//借助mapState生成计算属性，从state中读取数据。（对象写法）</span><br>	<span class="hljs-comment">// ...mapState(&#x27;student&#x27;, &#123;studentList:&#x27;studentList&#x27;&#125;),</span><br>	<span class="hljs-comment">// ...mapState(&#x27;student&#x27;, &#123;studentList&#125;), (简写) </span><br>	<br>	<span class="hljs-comment">//借助mapState生成计算属性，从state中读取数据。（数组写法）</span><br>	...<span class="hljs-title function_">mapState</span>(<span class="hljs-string">&#x27;student&#x27;</span>, [<span class="hljs-string">&#x27;studentList&#x27;</span>])<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3-4-2-mapGetter"><a href="#3-4-2-mapGetter" class="headerlink" title="3.4.2 mapGetter"></a>3.4.2 mapGetter</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用前，按需在对应的组件里引入。</span><br><span class="hljs-keyword">import</span> &#123;mapGetter&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vuex&#x27;</span><br><span class="hljs-comment">// 手动写法</span><br><span class="hljs-attr">computed</span>:&#123;<br>	<span class="hljs-title function_">studenCount</span>(<span class="hljs-params"></span>)&#123;<br>		<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">getters</span>[<span class="hljs-string">&#x27;student/studentCount&#x27;</span>]<br>	&#125;<br>&#125;<br><span class="hljs-comment">// 使用mapGetter</span><br><span class="hljs-attr">computed</span>:&#123;<br>	<span class="hljs-comment">//借助mapGetters生成计算属性，从getters中读取数据。（对象写法）</span><br>	<span class="hljs-comment">// ...mapGetters(&#123;studentCount:&#x27;studentCount&#x27;&#125;)</span><br>	<span class="hljs-comment">// ...mapGetters(&#123;studentCount&#125;) (简写) </span><br>	<br>	<span class="hljs-comment">//借助mapGetters生成计算属性，从getters中读取数据。（数组写法）</span><br>	...<span class="hljs-title function_">mapGetters</span>([<span class="hljs-string">&#x27;studentCount&#x27;</span>])<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3-4-3-mapMutations"><a href="#3-4-3-mapMutations" class="headerlink" title="3.4.3 mapMutations"></a>3.4.3 mapMutations</h3><p>mapMutations简写了commit方法，和之前的mapState和mapGetter不同，它是放在methods里的，在调用方法时需要传参，个人感觉这种简写比较鸡肋，容易和组件里的其他methods弄混。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用前，按需在对应的组件里引入。</span><br><span class="hljs-keyword">import</span> &#123;mapMutations&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vuex&#x27;</span><br><span class="hljs-comment">// 手动写法</span><br><span class="hljs-attr">methods</span>: &#123;<br>      <span class="hljs-comment">// 添加学生</span><br>      <span class="hljs-title function_">addStudent</span>(<span class="hljs-params"></span>)&#123;<br>          <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> === <span class="hljs-string">&#x27;&#x27;</span>) <br>            <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;学生名字不能为空~&#x27;</span>)<br>          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;student/addStudent&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span>)<br>          <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> = <span class="hljs-string">&#x27;&#x27;</span><br>      &#125;,<br>      <span class="hljs-comment">// 删除学生</span><br>      <span class="hljs-title function_">removeStuById</span>(<span class="hljs-params">id</span>)&#123;<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;student/removeStuById&#x27;</span>, id)<br>      &#125;<br>&#125;<br><span class="hljs-comment">// 使用 mapMutations</span><br><span class="hljs-attr">methods</span>: &#123;<br>	<span class="hljs-comment">//借助mapMutations生成对应的方法，方法中会调用commit去联系mutations(对象写法)</span><br>	...<span class="hljs-title function_">mapMutations</span>(<span class="hljs-string">&#x27;student&#x27;</span>,&#123;<span class="hljs-attr">removeStuById</span>:<span class="hljs-string">&#x27;removeStuById&#x27;</span>&#125;),<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="3-4-4-mapActions"><a href="#3-4-4-mapActions" class="headerlink" title="3.4.4 mapActions"></a>3.4.4 mapActions</h3><p>这个和之前的mapMutations一样，放在方法里的，不过它简写的是dispatch方法</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用前，按需在对应的组件里引入。</span><br><span class="hljs-keyword">import</span> &#123;mapActions&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vuex&#x27;</span><br><span class="hljs-comment">// 手动写法</span><br><span class="hljs-attr">methods</span>: &#123;<br>    <span class="hljs-title function_">search</span>(<span class="hljs-params"></span>)&#123;<br>      <span class="hljs-comment">// 筛选学生信息</span><br>      <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchType</span> === <span class="hljs-string">&#x27;id&#x27;</span>)&#123;           <span class="hljs-comment">// 根据ID查询</span><br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&#x27;search/searchStuById&#x27;</span>, [<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])<br>      &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchType</span> === <span class="hljs-string">&#x27;name&#x27;</span>)&#123; <span class="hljs-comment">// 根据学生名字查询</span><br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&#x27;search/searchStuByName&#x27;</span>, [<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])<br>      &#125;<br>      <span class="hljs-comment">// 显示结果条数</span><br>      <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchStudentList</span>.<span class="hljs-property">length</span>) <br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchResultText</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchStudentList</span>.<span class="hljs-property">length</span> + <span class="hljs-string">&#x27; 条&#x27;</span>           <br>      <span class="hljs-keyword">else</span><br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchResultText</span> = <span class="hljs-string">&#x27;没有查询到数据&#x27;</span><br>    &#125;<br>&#125;<br><span class="hljs-comment">// 使用 mapMutations</span><br><span class="hljs-attr">methods</span>: &#123;<br>  ...<span class="hljs-title function_">mapActions</span>(<span class="hljs-string">&#x27;search&#x27;</span>,[<span class="hljs-string">&#x27;searchStuById&#x27;</span>, <span class="hljs-string">&#x27;searchStuByName&#x27;</span>]),<br>  <span class="hljs-title function_">search</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-comment">// 筛选学生信息</span><br>    <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchType</span> === <span class="hljs-string">&#x27;id&#x27;</span>)&#123;           <span class="hljs-comment">// 根据ID查询</span><br>      <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">searchStuById</span>([<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])<br>    &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchType</span> === <span class="hljs-string">&#x27;name&#x27;</span>)&#123; <span class="hljs-comment">// 根据学生名字查询</span><br>      <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">searchStuByName</span>([<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])<br>    &#125;<br>    <span class="hljs-comment">// 显示结果条数</span><br>    <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchStudentList</span>.<span class="hljs-property">length</span>) <br>      <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchResultText</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchStudentList</span>.<span class="hljs-property">length</span> + <span class="hljs-string">&#x27; 条&#x27;</span>           <br>    <span class="hljs-keyword">else</span><br>      <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchResultText</span> = <span class="hljs-string">&#x27;没有查询到数据&#x27;</span><br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="3-4-5-总结"><a href="#3-4-5-总结" class="headerlink" title="3.4.5 总结"></a>3.4.5 总结</h3><p><strong>共同点：</strong></p>
<ul>
<li>都支持数组和对象两种写法。</li>
<li>都可以被任意组件通过$store里的属性调用</li>
</ul>
<p><strong>简写的区别：</strong></p>
<ul>
<li>mapState简写了 <code>$store.state.Vuex模块名.state变量名</code></li>
<li>mapGetter简写了 <code>$store.getters[&#39;Vuex模块名/getter函数名&#39;]</code></li>
<li>mapMutation 简写了 <code>$store.commit(&#39;Vuex模块名/函数名&#39;, 数据)</code></li>
<li>mapActions 简写了 <code>$sotre.dispath(&#39;Vuex模块名/函数名&#39;, 数据)</code></li>
</ul>
<p><strong>位置的区别</strong></p>
<ul>
<li>写在 computed的：mapState、mapGetters</li>
<li>写在 methods的：mapMutations、mapActions</li>
</ul>
<h1 id="四、基于Vue2的学生信息管理案例"><a href="#四、基于Vue2的学生信息管理案例" class="headerlink" title="四、基于Vue2的学生信息管理案例"></a>四、基于Vue2的学生信息管理案例</h1><hr>
<p>如下图所示，主要使用App组件引入自定义的Search.vue 和 Student.vue两个组件，分别用于搜索学生和学生管理。<br><img src="https://img-blog.csdnimg.cn/11d7eda039c64f54bdddb4a1f23e462f.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-1-全局事件总线方法代码实现"><a href="#4-1-全局事件总线方法代码实现" class="headerlink" title="4.1 全局事件总线方法代码实现"></a>4.1 全局事件总线方法代码实现</h2><hr>
<p>在全局事件总线的方法下，数据主要是通过$emit 发送出来，通过 $on 进行接收，上述组件的数据交互主要在搜索用户的时候，在Search.vue中搜索用户，然后Student.vue需要返回用户，查询一次的示意图如下：</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/3d9476bf452740d9a5535f22196fe6a9.png#pic_center">https://img-blog.csdnimg.cn/3d9476bf452740d9a5535f22196fe6a9.png#pic_center</a> &#x3D;80%x)</p>
<h3 id="4-1-1-Main-js"><a href="#4-1-1-Main-js" class="headerlink" title="4.1.1 Main.js"></a>4.1.1 Main.js</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./App.vue&#x27;</span><br><span class="hljs-comment">// // 引入 store</span><br><span class="hljs-comment">// import store from &#x27;./store/index&#x27;</span><br><span class="hljs-comment">// 关闭Vue的生产提示</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><br><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>),<br>  <span class="hljs-title function_">beforeCreate</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$bus</span> = <span class="hljs-variable language_">this</span><br>  &#125;<br>  <span class="hljs-comment">// store</span><br>&#125;).$mount(<span class="hljs-string">&#x27;#app&#x27;</span>)<br><br></code></pre></td></tr></table></figure>

<h3 id="4-1-2-App-vue"><a href="#4-1-2-App-vue" class="headerlink" title="4.1.2 App.vue"></a>4.1.2 App.vue</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container shadow col-md-6&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Search</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">h5</span>&gt;</span>查询学生<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">Search</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Student</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">h5</span>&gt;</span>学生管理<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">Student</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Student</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/Student.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Search</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/Search.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-comment">// npm i bootstrap</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;bootstrap/dist/js/bootstrap&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;bootstrap/dist/css/bootstrap.min.css&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">  <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">Student</span>, <span class="hljs-title class_">Search</span>&#125;,</span><br><span class="language-javascript">  <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">  &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">  <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">    <span class="hljs-attribute">text-align</span>: center;</span><br><span class="language-css">  &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="4-1-3-Student-vue"><a href="#4-1-3-Student-vue" class="headerlink" title="4.1.3 Student.vue"></a>4.1.3 Student.vue</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mb-3 row&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;student-input&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-form-label text-end&quot;</span>&gt;</span>学生名称<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-8&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;newStudentName&quot;</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">&quot;addStudent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;student-input&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;addStudent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary w-100 mb-3&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;table&quot;</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;studentList.length &gt; 0&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>ID<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>名字<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>操作<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(stu) in studentList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;stu.id&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>&#123;&#123; stu.id &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>&#123;&#123; stu.name &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;removeStuById(stu.id)&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger mx-3&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123; nanoid &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;nanoid&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Student&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-comment">// 若有本地缓存就读缓存，否则就初始化三个学生数据</span></span><br><span class="language-javascript">            <span class="hljs-attr">studentList</span>: </span><br><span class="language-javascript">              <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>)) || </span><br><span class="language-javascript">            [</span><br><span class="language-javascript">                &#123;</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-number">1</span>,</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;张三&#x27;</span>,</span><br><span class="language-javascript">                &#125;,</span><br><span class="language-javascript">                &#123;</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-number">2</span>,</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;李四&#x27;</span>,</span><br><span class="language-javascript">                &#125;,</span><br><span class="language-javascript">                &#123;</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;id&#x27;</span>:<span class="hljs-number">3</span>,</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&#x27;王五&#x27;</span>,</span><br><span class="language-javascript">                &#125;</span><br><span class="language-javascript">            ],</span><br><span class="language-javascript">            <span class="hljs-attr">newStudentName</span>: <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 添加学生</span></span><br><span class="language-javascript">        </span><br><span class="language-javascript">      <span class="hljs-title function_">addStudent</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">          <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> === <span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;学生名字不能为空~&#x27;</span>)</span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">unshift</span>(&#123;</span><br><span class="language-javascript">            <span class="hljs-attr">id</span>: <span class="hljs-title function_">nanoid</span>().<span class="hljs-title function_">substring</span>(<span class="hljs-number">0</span>, <span class="hljs-number">5</span>),</span><br><span class="language-javascript">            <span class="hljs-attr">name</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span>, </span><br><span class="language-javascript">          &#125;)</span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> = <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">      &#125;,</span><br><span class="language-javascript">      <span class="hljs-comment">// 删除学生</span></span><br><span class="language-javascript">      <span class="hljs-title function_">removeStuById</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(!<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除吗?&#x27;</span>)) <span class="hljs-keyword">return</span> </span><br><span class="language-javascript">        <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">filter</span>(</span><br><span class="language-javascript">          <span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span> stu.<span class="hljs-property">id</span> !== id</span><br><span class="language-javascript">        )</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 响应其他组件获取学生列表</span></span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;sendStudentListRequest&#x27;</span>, <span class="hljs-function">() =&gt;</span> &#123;</span><br><span class="language-javascript">          <span class="hljs-comment">// 发布学生列表数据</span></span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;getStudentList&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span>)</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">      )</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;sendStudentListRequest&#x27;</span>)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">  <span class="hljs-comment">// 监视属性值, 若有修改则将其放到本地缓存</span></span><br><span class="language-javascript">  <span class="hljs-attr">watch</span>: &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">studentList</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-attr">deep</span>: <span class="hljs-literal">true</span>,</span><br><span class="language-javascript">      <span class="hljs-title function_">handler</span>(<span class="hljs-params">value</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(value))</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="4-1-4-Search-vue"><a href="#4-1-4-Search-vue" class="headerlink" title="4.1.4 Search.vue"></a>4.1.4 Search.vue</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mb-3 row&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;student-input&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-form-label text-end&quot;</span>&gt;</span>学生名称<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-8&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;newStudentName&quot;</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">&quot;addStudent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;student-input&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;addStudent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary w-100 mb-3&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;table&quot;</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;studentList.length &gt; 0&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>ID<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>名字<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>操作<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(stu) in studentList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;stu.id&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>&#123;&#123; stu.id &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>&#123;&#123; stu.name &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;removeStuById(stu.id)&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger mx-3&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123; nanoid &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;nanoid&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Student&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-comment">// 若有本地缓存就读缓存，否则就初始化三个学生数据</span></span><br><span class="language-javascript">            <span class="hljs-attr">studentList</span>: </span><br><span class="language-javascript">              <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>)) || </span><br><span class="language-javascript">            [</span><br><span class="language-javascript">                &#123;</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-number">1</span>,</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;张三&#x27;</span>,</span><br><span class="language-javascript">                &#125;,</span><br><span class="language-javascript">                &#123;</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-number">2</span>,</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;李四&#x27;</span>,</span><br><span class="language-javascript">                &#125;,</span><br><span class="language-javascript">                &#123;</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;id&#x27;</span>:<span class="hljs-number">3</span>,</span><br><span class="language-javascript">                  <span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&#x27;王五&#x27;</span>,</span><br><span class="language-javascript">                &#125;</span><br><span class="language-javascript">            ],</span><br><span class="language-javascript">            <span class="hljs-attr">newStudentName</span>: <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 添加学生</span></span><br><span class="language-javascript">        </span><br><span class="language-javascript">      <span class="hljs-title function_">addStudent</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">          <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> === <span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;学生名字不能为空~&#x27;</span>)</span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">unshift</span>(&#123;</span><br><span class="language-javascript">            <span class="hljs-attr">id</span>: <span class="hljs-title function_">nanoid</span>().<span class="hljs-title function_">substring</span>(<span class="hljs-number">0</span>, <span class="hljs-number">5</span>),</span><br><span class="language-javascript">            <span class="hljs-attr">name</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span>, </span><br><span class="language-javascript">          &#125;)</span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> = <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">      &#125;,</span><br><span class="language-javascript">      <span class="hljs-comment">// 删除学生</span></span><br><span class="language-javascript">      <span class="hljs-title function_">removeStuById</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(!<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除吗?&#x27;</span>)) <span class="hljs-keyword">return</span> </span><br><span class="language-javascript">        <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">filter</span>(</span><br><span class="language-javascript">          <span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span> stu.<span class="hljs-property">id</span> !== id</span><br><span class="language-javascript">        )</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 响应其他组件获取学生列表</span></span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$on(<span class="hljs-string">&#x27;sendStudentListRequest&#x27;</span>, <span class="hljs-function">() =&gt;</span> &#123;</span><br><span class="language-javascript">          <span class="hljs-comment">// 发布学生列表数据</span></span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$emit(<span class="hljs-string">&#x27;getStudentList&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">studentList</span>)</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">      )</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-title function_">beforeDestroy</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">this</span>.<span class="hljs-property">$bus</span>.$off(<span class="hljs-string">&#x27;sendStudentListRequest&#x27;</span>)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">  <span class="hljs-comment">// 监视属性值, 若有修改则将其放到本地缓存</span></span><br><span class="language-javascript">  <span class="hljs-attr">watch</span>: &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">studentList</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-attr">deep</span>: <span class="hljs-literal">true</span>,</span><br><span class="language-javascript">      <span class="hljs-title function_">handler</span>(<span class="hljs-params">value</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(value))</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">  &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>


<h2 id="4-2-Vuex-方式代码实现"><a href="#4-2-Vuex-方式代码实现" class="headerlink" title="4.2 Vuex 方式代码实现"></a>4.2 Vuex 方式代码实现</h2><p><strong>运行效果：</strong><br><img src="https://img-blog.csdnimg.cn/bec65e716d4448e09405c83d3e969868.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>项目结构：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/4725f8cec9ef41dfad888492d5b5dbcd.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h3 id="4-2-1-main-js"><a href="#4-2-1-main-js" class="headerlink" title="4.2.1 main.js"></a>4.2.1 main.js</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">App</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./App.vue&#x27;</span><br><span class="hljs-comment">// // 引入 store</span><br><span class="hljs-keyword">import</span> store <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./store&#x27;</span><br><span class="hljs-comment">// 关闭Vue的生产提示</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-property">config</span>.<span class="hljs-property">productionTip</span> = <span class="hljs-literal">false</span><br><br><span class="hljs-keyword">new</span> <span class="hljs-title class_">Vue</span>(&#123;<br>  <span class="hljs-attr">el</span>: <span class="hljs-string">&#x27;#app&#x27;</span>,<br>  <span class="hljs-attr">render</span>: <span class="hljs-function"><span class="hljs-params">h</span> =&gt;</span> <span class="hljs-title function_">h</span>(<span class="hljs-title class_">App</span>),<br>  store,<br>  <span class="hljs-title function_">beforeCreate</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-title class_">Vue</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">$bus</span> = <span class="hljs-variable language_">this</span><br>  &#125;<br>&#125;)<br><br></code></pre></td></tr></table></figure>

<h3 id="4-2-2-store-x2F-index-js"><a href="#4-2-2-store-x2F-index-js" class="headerlink" title="4.2.2 store&#x2F;index.js"></a>4.2.2 store&#x2F;index.js</h3> <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs js"> <span class="hljs-comment">// 该文件用于创建 VueX 中最为核心的 Store</span><br><span class="hljs-comment">// 引入 Vue</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vue</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vue&#x27;</span><br><span class="hljs-comment">// 安装vuex@3 npm i vuex@3</span><br><span class="hljs-keyword">import</span> <span class="hljs-title class_">Vuex</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;vuex&#x27;</span><br><span class="hljs-keyword">import</span> search <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./search&#x27;</span><br><span class="hljs-keyword">import</span> student <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./student&#x27;</span><br><span class="hljs-title class_">Vue</span>.<span class="hljs-title function_">use</span>(<span class="hljs-title class_">Vuex</span>)<br><br><span class="hljs-comment">// 创建 Store并导出</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Vuex</span>.<span class="hljs-title class_">Store</span>(&#123;<br>    <span class="hljs-attr">modules</span>: &#123;search, student&#125;<br>&#125;)<br></code></pre></td></tr></table></figure>
<h3 id="4-2-3-store-x2F-student-js"><a href="#4-2-3-store-x2F-student-js" class="headerlink" title="4.2.3 store&#x2F;student.js"></a>4.2.3 store&#x2F;student.js</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">import</span> &#123; nanoid &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;nanoid&quot;</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>    <span class="hljs-attr">namespaced</span>:<span class="hljs-literal">true</span>,<br>    <span class="hljs-comment">// Actions —— 用于响应组件中的动作（若无Axios可不使用）</span><br>    <span class="hljs-attr">actions</span>: &#123;&#125;,<br>    <span class="hljs-comment">// Mutations —— 用于操作数据（state）</span><br>    <span class="hljs-attr">mutations</span>: &#123;<br>        <span class="hljs-comment">// 添加学生</span><br>        <span class="hljs-title function_">addStudent</span>(<span class="hljs-params">state, stuName</span>)&#123;<br>            state.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">unshift</span>(&#123;<br>                <span class="hljs-attr">id</span>: <span class="hljs-title function_">nanoid</span>().<span class="hljs-title function_">substring</span>(<span class="hljs-number">0</span>, <span class="hljs-number">5</span>),<br>                <span class="hljs-attr">name</span>: stuName, <br>            &#125;)<br>        &#125;,<br>        <span class="hljs-comment">// 根据ID删除学生</span><br>        <span class="hljs-title function_">removeStuById</span>(<span class="hljs-params">state, id</span>)&#123;<br>            state.<span class="hljs-property">studentList</span> = state.<span class="hljs-property">studentList</span>.<span class="hljs-title function_">filter</span>(<br>                <span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span> stu.<span class="hljs-property">id</span> !== id<br>            )<br>        &#125;<br>    &#125;,<br>    <span class="hljs-comment">// State —— 用于存储数据</span><br>    <span class="hljs-attr">state</span>: &#123;<br>        <span class="hljs-comment">// 若有本地缓存就读缓存，否则就初始化三个学生数据</span><br>        <span class="hljs-attr">studentList</span>: <br>        <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(<span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>)) || <br>        [<br>            &#123;<br>            <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-string">&#x27;1&#x27;</span>,<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;张三&#x27;</span>,<br>            &#125;,<br>            &#123;<br>            <span class="hljs-string">&#x27;id&#x27;</span>: <span class="hljs-string">&#x27;2&#x27;</span>,<br>            <span class="hljs-string">&#x27;name&#x27;</span>: <span class="hljs-string">&#x27;李四&#x27;</span>,<br>            &#125;,<br>            &#123;<br>            <span class="hljs-string">&#x27;id&#x27;</span>:<span class="hljs-string">&#x27;3&#x27;</span>,<br>            <span class="hljs-string">&#x27;name&#x27;</span>:<span class="hljs-string">&#x27;王五&#x27;</span>,<br>            &#125;<br>        ],<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="4-2-4-store-x2F-search-js"><a href="#4-2-4-store-x2F-search-js" class="headerlink" title="4.2.4 store&#x2F;search.js"></a>4.2.4 store&#x2F;search.js</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;<br>    <span class="hljs-attr">namespaced</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-comment">// Actions —— 用于响应组件中的动作</span><br>    <span class="hljs-attr">actions</span>: &#123;<br>        <span class="hljs-title function_">searchStuById</span>(<span class="hljs-params">context, info</span>)&#123;<br>            context.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;getStuById&#x27;</span>,info)<br>        &#125;,<br>        <span class="hljs-title function_">searchStuByName</span>(<span class="hljs-params">context, info</span>)&#123;<br>            context.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;getStuByName&#x27;</span>, info)<br>        &#125;<br>    &#125;,<br>    <span class="hljs-comment">// Mutations —— 用于操作数据（state）</span><br>    <span class="hljs-attr">mutations</span>: &#123;<br>        <span class="hljs-comment">// 根据ID查询学生</span><br>        <span class="hljs-title function_">getStuById</span>(<span class="hljs-params">state, info</span>)&#123;<br>            <span class="hljs-keyword">let</span> id = info[<span class="hljs-number">0</span>]<br>            <span class="hljs-keyword">let</span> studentList = info[<span class="hljs-number">1</span>]<br>            state.<span class="hljs-property">searchStudentList</span> = studentList.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span>  stu.<span class="hljs-property">id</span> === id)<br>        &#125;,<br>        <span class="hljs-comment">// 根据名字查询学生</span><br>        <span class="hljs-title function_">getStuByName</span>(<span class="hljs-params">state, info</span>)&#123;<br>            <span class="hljs-keyword">let</span> name = info[<span class="hljs-number">0</span>]<br>            <span class="hljs-keyword">let</span> studentList = info[<span class="hljs-number">1</span>]<br>            state.<span class="hljs-property">searchStudentList</span> = studentList.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">stu</span>) =&gt;</span> stu.<span class="hljs-property">name</span>.<span class="hljs-title function_">indexOf</span>(name) &gt; -<span class="hljs-number">1</span> )<br>        &#125;<br>    &#125;,<br>    <span class="hljs-comment">// State —— 用于存储数据</span><br>    <span class="hljs-attr">state</span>: &#123;<br>        <span class="hljs-attr">searchStudentList</span>: [],<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="4-2-5-App-vue"><a href="#4-2-5-App-vue" class="headerlink" title="4.2.5 App.vue"></a>4.2.5 App.vue</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container shadow col-md-6&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Search</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">h5</span>&gt;</span>查询学生<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">Search</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">Student</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">h5</span>&gt;</span>学生管理<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">Student</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Student</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/Student.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-title class_">Search</span> <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;./components/Search.vue&#x27;</span></span><br><span class="language-javascript"><span class="hljs-comment">// npm i bootstrap</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;bootstrap/dist/js/bootstrap&#x27;</span></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">&#x27;bootstrap/dist/css/bootstrap.min.css&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">  <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;App&#x27;</span>,</span><br><span class="language-javascript">  <span class="hljs-attr">components</span>: &#123;<span class="hljs-title class_">Student</span>, <span class="hljs-title class_">Search</span>&#125;,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">scoped</span>&gt;</span><span class="language-css"></span><br><span class="language-css">  <span class="hljs-selector-tag">div</span>&#123;</span><br><span class="language-css">    <span class="hljs-attribute">text-align</span>: center;</span><br><span class="language-css">  &#125;</span><br><span class="language-css"></span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h3 id="4-2-6-Student-vue"><a href="#4-2-6-Student-vue" class="headerlink" title="4.2.6 Student.vue"></a>4.2.6 Student.vue</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mb-3 row&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;student-input&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2 col-form-label text-end&quot;</span>&gt;</span>学生名称<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-8&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;newStudentName&quot;</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">&quot;addStudent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;student-input&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-sm-2&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;addStudent&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary w-100 mb-3&quot;</span>&gt;</span>添加<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;table&quot;</span> <span class="hljs-attr">v-show</span>=<span class="hljs-string">&quot;studentList.length &gt; 0&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>ID<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>名字<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;col&quot;</span>&gt;</span>操作<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(stu) in studentList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;stu.id&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">th</span> <span class="hljs-attr">scope</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>&#123;&#123; stu.id &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>&#123;&#123; stu.name &#125;&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;removeStuById(stu.id)&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-danger mx-3&quot;</span>&gt;</span>删除<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>          <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">import</span> &#123; nanoid &#125; <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;nanoid&#x27;</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Student&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">            <span class="hljs-attr">newStudentName</span>: <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 添加学生</span></span><br><span class="language-javascript">      <span class="hljs-title function_">addStudent</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">          <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> === <span class="hljs-string">&#x27;&#x27;</span>) </span><br><span class="language-javascript">            <span class="hljs-keyword">return</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;学生名字不能为空~&#x27;</span>)</span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;student/addStudent&#x27;</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span>)</span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">newStudentName</span> = <span class="hljs-string">&#x27;&#x27;</span></span><br><span class="language-javascript">      &#125;,</span><br><span class="language-javascript">      <span class="hljs-comment">// 删除学生</span></span><br><span class="language-javascript">      <span class="hljs-title function_">removeStuById</span>(<span class="hljs-params">id</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(!<span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;确认要删除吗?&#x27;</span>)) <span class="hljs-keyword">return</span> </span><br><span class="language-javascript">        <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">commit</span>(<span class="hljs-string">&#x27;student/removeStuById&#x27;</span>, id)</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">computed</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-comment">// 计算属性: 获取store里的值</span></span><br><span class="language-javascript">      <span class="hljs-title function_">studentList</span>(<span class="hljs-params"></span>)&#123; </span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">state</span>.<span class="hljs-property">student</span>.<span class="hljs-property">studentList</span> </span><br><span class="language-javascript">      &#125; </span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">  <span class="hljs-comment">// 监视studentList属性值, 若有修改则将其放到本地缓存</span></span><br><span class="language-javascript">    <span class="hljs-attr">watch</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-attr">studentList</span>:&#123;</span><br><span class="language-javascript">        <span class="hljs-attr">deep</span>: <span class="hljs-literal">true</span>,</span><br><span class="language-javascript">        <span class="hljs-title function_">handler</span>(<span class="hljs-params">value</span>)&#123;</span><br><span class="language-javascript">          <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&#x27;studentList&#x27;</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(value))</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h3 id="4-2-7-Search-vue"><a href="#4-2-7-Search-vue" class="headerlink" title="4.2.7 Search.vue"></a>4.2.7 Search.vue</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">slot</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">slot</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;row g-3&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-auto&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;staticEmail2&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;visually-hidden&quot;</span>&gt;</span>学生名称<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-select&quot;</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;searchType&quot;</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;id&quot;</span>&gt;</span>ID<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>          <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;name&quot;</span>&gt;</span>名字<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-auto&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;input-search&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;visually-hidden&quot;</span>&gt;</span>学生名字<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">&quot;searchInfo&quot;</span> @<span class="hljs-attr">keydown.enter</span>=<span class="hljs-string">&quot;search&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;input-search&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;请输入查询内容&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;col-auto&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> @<span class="hljs-attr">click</span>=<span class="hljs-string">&quot;search&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary mb-3&quot;</span>&gt;</span>搜索<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-start&quot;</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>查询结果: &#123;&#123; searchResultText &#125;&#125; <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;(stu) in searchStudentList&quot;</span> <span class="hljs-attr">:key</span>=<span class="hljs-string">&quot;stu.id&quot;</span>&gt;</span><br>          &#123;&#123;stu.id&#125;&#125;, &#123;&#123;stu.name&#125;&#125;<br>        <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span><br>      <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> &#123;</span><br><span class="language-javascript">    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Score&#x27;</span>,</span><br><span class="language-javascript">    <span class="hljs-title function_">data</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-keyword">return</span> &#123;</span><br><span class="language-javascript">        <span class="hljs-attr">searchType</span>: <span class="hljs-string">&#x27;id&#x27;</span>,</span><br><span class="language-javascript">        <span class="hljs-attr">searchInfo</span>: <span class="hljs-string">&#x27;&#x27;</span>,</span><br><span class="language-javascript">        <span class="hljs-attr">searchResultText</span>: <span class="hljs-string">&#x27;暂未查询&#x27;</span>,</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-title function_">mounted</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>)</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">computed</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-title function_">searchStudentList</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">state</span>.<span class="hljs-property">search</span>.<span class="hljs-property">searchStudentList</span> || []</span><br><span class="language-javascript">      &#125;,</span><br><span class="language-javascript">      <span class="hljs-title function_">allStudent</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-property">state</span>.<span class="hljs-property">student</span>.<span class="hljs-property">studentList</span> || []</span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">    <span class="hljs-attr">methods</span>: &#123;</span><br><span class="language-javascript">      <span class="hljs-title function_">search</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-comment">// 筛选学生信息</span></span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchType</span> === <span class="hljs-string">&#x27;id&#x27;</span>)&#123;           <span class="hljs-comment">// 根据ID查询</span></span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&#x27;search/searchStuById&#x27;</span>, [<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])</span><br><span class="language-javascript">        &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchType</span> === <span class="hljs-string">&#x27;name&#x27;</span>)&#123; <span class="hljs-comment">// 根据学生名字查询</span></span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">$store</span>.<span class="hljs-title function_">dispatch</span>(<span class="hljs-string">&#x27;search/searchStuByName&#x27;</span>, [<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchInfo</span>, <span class="hljs-variable language_">this</span>.<span class="hljs-property">allStudent</span>])</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">        <span class="hljs-comment">// 显示结果条数</span></span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">searchStudentList</span>.<span class="hljs-property">length</span>) </span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchResultText</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchStudentList</span>.<span class="hljs-property">length</span> + <span class="hljs-string">&#x27; 条&#x27;</span>           </span><br><span class="language-javascript">        <span class="hljs-keyword">else</span></span><br><span class="language-javascript">          <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchResultText</span> = <span class="hljs-string">&#x27;没有查询到数据&#x27;</span></span><br><span class="language-javascript">      &#125;</span><br><span class="language-javascript">    &#125;,</span><br><span class="language-javascript">&#125;</span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br></code></pre></td></tr></table></figure>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%89%8D%E7%AB%AF/" class="category-chain-item">前端</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Vue/">#Vue</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据</div>
      <div>http://example.com/2022/06/24/Vue2 笔记4 基于Vue2的学生信息增删查案例 _ Vuex组件简介、工作原理 _ Vuex的三层结构——store负责响应组件、mutations负责操作数据、state负责存储数据/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年6月24日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/06/25/Vue2%20%E7%AC%94%E8%AE%B05%20Vue2%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC%E9%97%AE%E9%A2%98%20_%20vue-router%E8%B7%AF%E7%94%B1%E6%A6%82%E5%BF%B5%E3%80%81%E5%88%86%E7%B1%BB%E4%B8%8E%E4%BD%BF%E7%94%A8%20_%20%E7%BC%96%E7%A8%8B%E5%BC%8F%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%20_%20%E8%B7%AF%E7%94%B1%E7%BB%84%E4%BB%B6%E7%9A%84%E7%BC%93%E5%AD%98%20_%205%E7%A7%8D%E8%B7%AF%E7%94%B1%E5%AF%BC%E8%88%AA%E5%AE%88%E5%8D%AB%20_%20%E5%B5%8C%E5%A5%97%E8%B7%AF%E7%94%B1%20_%20Vue2%E9%A1%B9%E7%9B%AE%E7%9A%84%E6%89%93%E5%8C%85%E4%B8%8E%E9%83%A8%E7%BD%B2/" title="Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Vue2 笔记5 Vue2页面跳转问题 _ vue-router路由概念、分类与使用 _ 编程式路由导航 _ 路由组件的缓存 _ 5种路由导航守卫 _ 嵌套路由 _ Vue2项目的打包与部署</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/06/21/Vue2%20%E7%AC%94%E8%AE%B02%20%E5%9F%BA%E4%BA%8EVue2%E7%9A%84TodoList%E5%BE%85%E5%8A%9E%E4%BA%8B%E9%A1%B9%E6%A1%88%E4%BE%8B%20_%20localStorage%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8%20_%20Vue2%E7%9A%84%E5%8F%91%E5%B8%83%E4%B8%8E%E8%AE%A2%E9%98%85%20_%20Vue2%E6%94%AF%E6%8C%81%E7%9A%84%E5%8A%A8%E7%94%BB%E7%B1%BB%20_JavaScript%E5%8E%9F%E5%9E%8B%E5%AF%B9%E8%B1%A1/" title="Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象">
                        <span class="hidden-mobile">Vue2 笔记2 基于Vue2的TodoList待办事项案例 _ localStorage本地存储 _ Vue2的发布与订阅 _ Vue2支持的动画类 _JavaScript原型对象</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
